<?php
/**
 * $Id: config.php 27 2009-06-27 02:26:17Z eyou80 $
 */

//require_once 'tinyask/app.php';

/**
 * 设置类
 *
 * @package App
 */
class App_Config extends Tinyask_App
{
	/**
	 * 缓存的插件配置
	 *
	 * @var array
	 */
	private $_pluginConfig = array();

	/**
	 * 数据库对象
	 *
	 * @var instance of  Tinyask_Db
	 */
	protected $db;

	public function __construct()
	{
		$this->db = Tinyask_Db::get();
	}

	/**
	 * 获取解析路径前缀
	 *
	 * @return string
	 */
	protected function __index()
	{
		return $this->rewrite ? $this->siteUrl : Tinyask_Common::url('index.php', $this->siteUrl);
	}

	/**
	 * 获取模板路径
	 *
	 * @return string
	 */
	protected function __themeUrl()
	{
		return Tinyask_Common::url(__TINYASK_THEME_DIR__ . '/' . $this->theme, $this->siteUrl);
	}

	/**
	 * 获取插件路径
	 *
	 * @return string
	 */
	protected function __pluginUrl()
	{
		return Tinyask_Common::url(__TINYASK_PLUGIN_DIR__, $this->siteUrl);
	}

	/**
	 * 获取后台路径
	 *
	 * @return string
	 */
	protected function __adminUrl()
	{
		return Tinyask_Common::url(defined('__TINYASK_ADMIN_DIR__') ? __TINYASK_ADMIN_DIR__ : '/admin/', $this->siteUrl);
	}

	/**
	 * 获取资源路径
	 *
	 * @return string
	 */
	protected function __resourceUrl()
	{
		return Tinyask_Common::url(defined('__TINYASK_RESOURCE_DIR__') ? __TINYASK_RESOURCE_DIR__ : '/usr/resource/', $this->siteUrl);
	}

	/**
	 * 获取个人档案路径
	 *
	 * @return unknown
	 */
	protected function __profileUrl()
	{
		return Tinyask_Common::url('profile.php', $this->siteUrl);
	}

	/**
	 * 获取编码
	 *
	 * @return string
	 */
	protected function __charset()
	{
		return Tinyask_Common::$config['charset'];
	}

	/**
	 * 获取系统时区
	 *
	 * @return integer
	 */
	protected function __serverTimezone()
	{
		return Tinyask_Date::$serverTimezone;
	}

	/**
	 * 获取格林威治时间
	 *
	 * @return integer
	 */
	protected function __gmtTime()
	{
		return Tinyask_Date::gmtTime();
	}

	/**
	 * 执行
	 *
	 */
	public function execute()
	{
		$this->db->fetchAll($this->db->select('name', 'value')->from('table.config'), array($this, 'push'));
		$this->stack[] = &$this->row;

		/** 初始化站点信息 **/
		$this->siteUrl = Tinyask_Common::url(null, $this->siteUrl);
		$this->plugins = unserialize($this->plugins);

		/** 初始化路由并缓存 **/
		$this->routingTable = unserialize($this->routingTable);
		if(!isset($this->routingTable[0])) {
			$parser = new Tinyask_Router_Parser($this->routingTable);
			$parserdRoutingTable = $parser->parse();
			$this->routingTable = array_merge(array($parserdRoutingTable), $this->routingTable);
			$this->app('App_Abstract_config')->update(array('value' => serialize($this->routingTable)), $this->db->sql()->where('name = ?', 'routingTable'));
		}
	}

	/**
	 * 将变量压入堆栈
	 *
	 * @param array $value
	 * @return array
	 */
	public function push(array $value)
	{
		$this->row[$value['name']] = $value['value'];
		return $value;
	}

	/**
	 * 输出网站路径
	 *
	 * @param string $path
	 */
	public function siteUrl($path = null)
	{
		echo Tinyask_Common::url($path, $this->siteUrl);
	}

	/**
	 * 输出解析地址
	 *
	 * @param string $path
	 */
	public function index($path = null)
	{
		echo Tinyask_Common::url($path, $this->index);
	}

	/**
	 * 输出模板地址
	 *
	 * @param string $path
	 */
	public function themeUrl($path = null)
	{
		echo Tinyask_Common::url($path, $this->themeUrl);
	}

	/**
	 * 输出后台地址
	 *
	 * @param string $path
	 */
	public function adminUrl($path =null)
	{
		echo Tinyask_Common::url($path, $this->adminUrl);
	}

	public function plugin($pluginName = null)
	{
		if(empty($pluginName)) {
			return parent::plugin();
		}

		if(!isset($this->_pluginConfig[$pluginName])) {
			if(!empty($this->row['plugin:' . $pluginName])
			&& ($config = unserialize($this->row['plugin:' . $pluginName])) !== false) {
				$this->_pluginConfig[$pluginName] = new Tinyask_Config($config);
			} else {
				throw new Tinyask_Plugin_Exception(_t('插件%s的配置信息没有找到', $pluginName), 500);
			}
		}

		return $this->_pluginConfig[$pluginName];
	}
}
